package controller

import (
	"net/http"
	"web_server/controller/mainctrl"
	"web_server/controller/specctrl"
	"web_server/controller/spikectrl"
	"web_server/controller/spikemanctrl"
	"web_server/controller/userctrl"
	"web_server/controller/userspecctrl"
	"web_server/model/mainmodel"
	"web_server/utils"

	"github.com/gin-gonic/gin"
)

type ActionFunc func(ctx *gin.Context, user mainmodel.UserModel)

// 注册相关的url
func SetupRouter(router *gin.Engine) {
	//登录相关
	router.GET("/spike4fruit/login", mainctrl.Login)
	router.GET("/spike4fruit/logout", mainctrl.Logout)
	router.POST("/spike4fruit/login", mainctrl.LoginPost)

	//index页和欢迎页面
	RegsterPageGetNoPerm(router, "index", mainctrl.Index)
	RegsterPageGetNoPerm(router, "welcome", mainctrl.Welcome)

	//修改自己密码
	RegsterPageGetNoPerm(router, "editpsw", mainctrl.EditPsw)
	RegsterPagePostNoPerm(router, "editpsw", mainctrl.EditPswPost)

	//抢水果页面
	RegsterPageGetNoPerm(router, "spike", spikectrl.Spike)
	RegsterPagePostNoPerm(router, "spike", spikectrl.SpikePost)

	//抢水果管理页面
	RegsterPageGet(router, "spikeman", "spikeman", spikemanctrl.SpikeMan)
	RegsterPagePost(router, "spikeman/*action", "spikeman", spikemanctrl.SpikeManPost)

	//用户管理
	RegsterTable(router, "user",
		userctrl.UserAdd, userctrl.UserEdt, userctrl.UserLst,
		userctrl.UserAddPost, userctrl.UserEdtPost, userctrl.UserDelPost,
	)
	//水果类别管理
	RegsterTable(router, "spec",
		specctrl.SpecAdd, specctrl.SpecEdt, specctrl.SpecLst,
		specctrl.SpecAddPost, specctrl.SpecEdtPost, specctrl.SpecDelPost,
	)
	//用户水果历史分配管理
	RegsterTable(router, "userspec",
		userspecctrl.UserSpecAdd, userspecctrl.UserSpecEdt, userspecctrl.UserSpecLst,
		userspecctrl.UserSpecAddPost, userspecctrl.UserSpecEdtPost, userspecctrl.UserSpecDelPost,
	)
}

func RegsterPageGetNoPerm(router *gin.Engine, page string, action ActionFunc) {
	router.GET("/spike4fruit/"+page, func(ctx *gin.Context) {
		_, val := utils.GetSession(ctx)
		if val == nil {
			ctx.Redirect(http.StatusFound, "/spike4fruit/login")
			return
		}
		user := val.(mainmodel.UserModel)
		action(ctx, user)
	})
}

func RegsterPagePostNoPerm(router *gin.Engine, page string, action ActionFunc) {
	router.POST("/spike4fruit/"+page, func(ctx *gin.Context) {
		_, val := utils.GetSession(ctx)
		if val == nil {
			ctx.Status(http.StatusNotFound)
			return
		}
		user := val.(mainmodel.UserModel)
		action(ctx, user)
	})
}

func RegsterPageGet(router *gin.Engine, page string, permurl string, action ActionFunc) {
	router.GET("/spike4fruit/"+page, func(ctx *gin.Context) {
		_, val := utils.GetSession(ctx)
		if val == nil {
			ctx.Redirect(http.StatusFound, "/spike4fruit/login")
			return
		}
		user := val.(mainmodel.UserModel)
		if !mainmodel.CheckRolePerm(user.Role, mainmodel.CheckPerm(permurl+"/get")) {
			ctx.String(http.StatusOK, "没有对应权限!")
		} else {
			action(ctx, user)
		}
	})
}

func RegsterPagePost(router *gin.Engine, page string, permurl string, action ActionFunc) {
	router.POST("/spike4fruit/"+page, func(ctx *gin.Context) {
		_, val := utils.GetSession(ctx)
		if val == nil {
			ctx.Status(http.StatusNotFound)
			return
		}
		user := val.(mainmodel.UserModel)
		if !mainmodel.CheckRolePerm(user.Role, mainmodel.CheckPerm(permurl+"/post")) {
			ctx.String(http.StatusOK, "没有对应权限!")
		} else {
			action(ctx, user)
		}
	})
}

func RegsterTable(router *gin.Engine, page string, add, edt, lst, addPost, edtPost, delPost ActionFunc) {
	router.GET("/spike4fruit/"+page+"/*action", func(ctx *gin.Context) {
		_, val := utils.GetSession(ctx)
		if val == nil {
			ctx.Redirect(http.StatusFound, "/spike4fruit/login")
			return
		}
		user := val.(mainmodel.UserModel)
		action := ctx.Param("action")
		switch action {
		case "/add":
			if add == nil {
				ctx.Status(http.StatusNotFound)
				break
			}
			if !mainmodel.CheckRolePerm(user.Role, mainmodel.CheckPerm(page+"/add")) {
				ctx.String(http.StatusOK, "没有对应权限!")
			} else {
				add(ctx, user)
			}
		case "/edit":
			if edt == nil {
				ctx.Status(http.StatusNotFound)
				break
			}
			if !mainmodel.CheckRolePerm(user.Role, mainmodel.CheckPerm(page+"/edit")) {
				ctx.String(http.StatusOK, "没有对应权限!")
			} else {
				edt(ctx, user)
			}
		case "/", "/list":
			if lst == nil {
				ctx.Status(http.StatusNotFound)
				break
			}
			if !mainmodel.CheckRolePerm(user.Role, mainmodel.CheckPerm(page+"/list")) {
				ctx.String(http.StatusOK, "没有对应权限!")
			} else {
				lst(ctx, user)
			}
		default:
			ctx.Status(http.StatusNotFound)
		}
	})
	router.POST("/spike4fruit/"+page+"/*action", func(ctx *gin.Context) {
		_, val := utils.GetSession(ctx)
		if val == nil {
			ctx.Status(http.StatusNotFound)
			return
		}
		user := val.(mainmodel.UserModel)
		action := ctx.Param("action")
		switch action {
		case "/add":
			if addPost == nil {
				ctx.Status(http.StatusNotFound)
				break
			}
			if !mainmodel.CheckRolePerm(user.Role, mainmodel.CheckPerm(page+"/add")) {
				ctx.String(http.StatusOK, "没有对应权限!")
			} else {
				addPost(ctx, user)
			}
		case "/edit":
			if edtPost == nil {
				ctx.Status(http.StatusNotFound)
				break
			}
			if !mainmodel.CheckRolePerm(user.Role, mainmodel.CheckPerm(page+"/edit")) {
				ctx.String(http.StatusOK, "没有对应权限!")
			} else {
				edtPost(ctx, user)
			}
		case "/del":
			if delPost == nil {
				ctx.Status(http.StatusNotFound)
				break
			}
			if !mainmodel.CheckRolePerm(user.Role, mainmodel.CheckPerm(page+"/del")) {
				ctx.String(http.StatusOK, "没有对应权限!")
			} else {
				delPost(ctx, user)
			}
		default:
			ctx.Status(http.StatusNotFound)
		}
	})
}
